package com.zhanggm.student.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhanggm.student.config.ResponseResult;
import com.zhanggm.student.entity.User;
import com.zhanggm.student.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2020-10-21
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private  IUserService iUserService;

    @PostMapping("login")
    public ResponseResult login(@RequestBody User user){
        //验证用户名或密码是否为空
        if(StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())){
            return ResponseResult.fail(10000,"用户名或密码不能为空");
        }
        //验证用户是否存在
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",user.getUsername());
        User userInfo = iUserService.getOne(queryWrapper);
        if(userInfo==null){
            return ResponseResult.fail(10000,"用户不存在");
        }
        //验证密码是否正确
        if(!user.getPassword().equals(userInfo.getPassword())){
            return ResponseResult.fail(10000,"用户名或密码错误");
        }
        //如果验证通过，给前端返回用户登录名称或用户Id
        userInfo.setPassword(null);
        return ResponseResult.success(userInfo);
    }

}
